// pages/mine/spread/spread.js
const app = getApp(),
    globalData = app.globalData,
    api = require('../../../api/index'),
    utils = require('../../../utils/util');

Page({

    /**
     * 页面的初始数据
     */
    data: {
        list: [],
        status: 0,
        hasMore: false,
        filter: {
            keywords: "",
            brandId: "",
            categoryId: "",
            types: [1, 2, 3],
            goodsOnState: 1,
            goodsSortByEnum: 1,
            currenetPageIndex: 1,
            pageSize: 10,
            recordCount: 0
        },
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.reload()
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
        if (this.data.hasMore) {
            this.setData({
                'filter.currenetPageIndex': ++this.data.filter.currenetPageIndex
            });

            this.getGoodsList();
        }
    },

    /**
     * 用户点击右上角分享
     */
    // onShareAppMessage: function (e) {
    //     console.log(e,'分享e',this.data.shareInfo, 'shareInfo')
    //     let userId = globalData.userInfo.UserId || '';
    //     console.log(`/pages/product/detail/detail?userId=${userId}`, '推广商品列表分享链接')
    //     return {
    //         title: this.data.shareInfo.Title,
    //         path: `/pages/product/detail/detail?userId=${userId}`,
    //         imageUrl: this.data.shareInfo.GoodsFileDtos[0].FilePath
    //     }
    // },

    reload() {
        this.setData({
            status: 0
        })
        this.getGoodsList();
    },

    // 获取商品列表
    async getGoodsList() {
        let obj = this.data.filter;
        wx.showLoading({
            title: '内容加载中',
        })
        try {
            let res = await api.goods.getGoodsPages(obj),
                data = res.data.ResData || {};
            console.log(res, '商品列表')
            if (this.data.filter.currenetPageIndex > 1) {
                data.Items = this.data.list.concat(data.Items);
            }
            this.setData({
                list: data.Items || [],
                hasMore: data.TotalPages == 0 ? false : data.CurrentPage == data.TotalPages ? false : true,
                status: 2
            })

        } catch (e) {
            console.log(e, '获取商品类别失败')
            this.setData({
                status: 1
            })
        } finally {
            wx.hideLoading()
        }
    },

    share(e) {
        console.log(e, '分享信息')
        this.setData({
            shareInfo: e.detail.value
        })
    }


})